﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 护士工作站
{
    public partial class 医嘱处理 : Form
    {
        public 医嘱处理()
        {
            InitializeComponent();
           
        }
        
        

        private void button_tc_Click(object sender, EventArgs e)
        {
            this.Hide();
            
        }

        private void button_qp_Click(object sender, EventArgs e)
        {

            tb_ch.Text = "";
            tb_nl.Text = "";
            tb_fyye.Text = "";
            tb_xb.Text = "";
            tb_xm.Text = "";
            tb_zd.Text = "";
            tb_jzys.Text = "";
            tb_ZYNo.Text = "";
            DataTable dt = (DataTable)dataGridView1.DataSource;
            dt.Rows.Clear();
            dataGridView1.DataSource = dt;
           
        }

 
        private void button_xz_Click(object sender, EventArgs e)
        {
            this.Hide();
             新增医嘱处理 c = new 新增医嘱处理();
           
            c.Show();

            新增医嘱处理.f1.tb_ZYNo.Text = tb_ZYNo.Text;
            新增医嘱处理.f1.tb_jzys.Text = tb_jzys.Text;
            新增医嘱处理.f1.tb_zd.Text = tb_zd.Text;
            新增医嘱处理.f1.tb_xm.Text = tb_xm.Text;
            新增医嘱处理.f1.tb_xb.Text = tb_xb.Text;
            新增医嘱处理.f1.tb_fyye.Text = tb_fyye.Text;
            新增医嘱处理.f1.tb_ch.Text = tb_ch.Text;
            新增医嘱处理.f1.tb_nl.Text = tb_nl.Text;

        }
      public DataSet ds = new DataSet();

        private void button_cx_Click(object sender, EventArgs e)
        {

            string ZYNo = tb_ZYNo.Text.Trim();


            if (ZYNo == "")
            {
                MessageBox.Show("请输入要查询的内容！");
                tb_ZYNo.Focus();
                tb_ch.Focus();

                return;
            }


            string Hos = "select * from InHospital,Patient where InHospital.HosNo='" + ZYNo + "'and InHospital.HosNo=Patient.HosNo ";

            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {

                try
                {
                    conn.Open();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                SqlCommand cmd = new SqlCommand(Hos, conn);


                if (ZYNo != "")
                {
                    SqlDataReader dr = cmd.ExecuteReader();

                    if (dr.HasRows)
                    {
                        dr.Read();


                        int nf = Convert.ToInt32(dr["BronYear"].ToString());
                        DateTime time = System.DateTime.Now;
                        int year = time.Year;
                        int nl = year - nf;
                        tb_nl.Text = nl.ToString();
                        tb_fyye.Text = dr["HosBalance"].ToString();
                        tb_xb.Text = dr["PatSex"].ToString();
                        tb_xm.Text = dr["PatName"].ToString();
                        tb_ch.Text = dr["BedNo"].ToString();
                        tb_zd.Text = dr["HosIllness"].ToString();

                        string dno = dr["DocNo"].ToString();
                        dr.Close();
                        string DNo = "select DocName from Doctor where  DocNo='" + dno + "' ";
                        SqlCommand D = new SqlCommand(DNo, conn);
                        string DD = Convert.ToString(D.ExecuteScalar());
                        tb_jzys.Text = DD;

                    }
                    dr.Close();
                }
            }
            string str = "select Condition from Patient where HosNo='" + ZYNo + "' ";

        
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                conn.Open();
                SqlCommand c = new SqlCommand(str, conn);
                string cc = Convert.ToString(c.ExecuteScalar());
                string sql = "select WarnNo, WarNature,WarType,WarBegindate,WarContent,WarDosage,WarWay,WarTime,WarFinishdate,WarExecutedate from Warn where Warn.HosNo='" + ZYNo + "'";
                 SqlDataAdapter da = new SqlDataAdapter();
               
                if (cc == "住院")
                {
                    da = new SqlDataAdapter(sql, conn);
                   
                    da.Fill(ds);
                    dataGridView1.DataSource = ds.Tables[0];
                }
                
                else
                {
                    if (cc == "转入")
                    {

                        da = new SqlDataAdapter(sql, conn);
                       
                        da.Fill(ds);
                        dataGridView1.DataSource = ds.Tables[0];
                    }
                    else
                    {
                        MessageBox.Show("此人已出院或转出");
                    }
                }

            }



        }

        private void rb_clqb_CheckedChanged(object sender, EventArgs e)
        {
            string sql = "select WarnNo, WarNature,WarType,WarBegindate,WarContent,WarDosage,WarWay,WarTime,WarFinishdate,WarExecutedate from Warn where Warn.HosNo='" + this.tb_ZYNo.Text + "'";
            if (rb_clqb.Checked == true)


                using (SqlConnection conn = new SqlConnection(DBHelper.connString))
                {
                    SqlDataAdapter da = new SqlDataAdapter();
                   
                    da = new SqlDataAdapter(sql, conn);
                    
                    da.Fill(ds);
                    dataGridView1.DataSource = ds.Tables[0];
                }
        }

        private void rb_cq_CheckedChanged(object sender, EventArgs e)
        {
            string sql = "select WarnNo, WarNature,WarType,WarBegindate,WarContent,WarDosage,WarWay,WarTime,WarFinishdate,WarExecutedate from Warn where Warn.HosNo='" + this.tb_ZYNo.Text + "'and WarNature='长期'";
            if (rb_cq.Checked == true)


                using (SqlConnection conn = new SqlConnection(DBHelper.connString))
                {
                    SqlDataAdapter da = new SqlDataAdapter();
                  
                    da = new SqlDataAdapter(sql, conn);
                    
                    da.Fill(ds);
                    dataGridView1.DataSource = ds.Tables[0];
                }
        }

        private void rb_ls_CheckedChanged(object sender, EventArgs e)
        {
            string sql = "select WarnNo, WarNature,WarType,WarBegindate,WarContent,WarDosage,WarWay,WarTime,WarFinishdate,WarExecutedate from Warn where Warn.HosNo='" + this.tb_ZYNo.Text + "'and WarNature='临时'";
            if (rb_ls.Checked == true)


                using (SqlConnection conn = new SqlConnection(DBHelper.connString))
                {
                    SqlDataAdapter da = new SqlDataAdapter();
                 ;
                    da = new SqlDataAdapter(sql, conn);
                 
                    da.Fill(ds);
                    dataGridView1.DataSource = ds.Tables[0];
                }
        }

        private void rb_yzqb_CheckedChanged(object sender, EventArgs e)
        {
            string sql = "select WarnNo, WarNature,WarType,WarBegindate,WarContent,WarDosage,WarWay,WarTime,WarFinishdate,WarExecutedate from Warn where Warn.HosNo='" + this.tb_ZYNo.Text + "'";
            if (rb_yzqb.Checked == true)


                using (SqlConnection conn = new SqlConnection(DBHelper.connString))
                {
                    SqlDataAdapter da = new SqlDataAdapter();
                    
                    da = new SqlDataAdapter(sql, conn);
                   
                    da.Fill(ds);
                    dataGridView1.DataSource = ds.Tables[0];
                }




        }

        private void rb_xk_CheckedChanged(object sender, EventArgs e)
        {
            string sql = "select WarnNo, WarNature,WarType,WarBegindate,WarContent,WarDosage,WarWay,WarTime,WarFinishdate,WarExecutedate from Warn where Warn.HosNo='" + this.tb_ZYNo.Text + "'and WarnCondition='新开'";
            if (rb_xk.Checked == true)


                using (SqlConnection conn = new SqlConnection(DBHelper.connString))
                {
                    SqlDataAdapter da = new SqlDataAdapter();
                    DataSet ds = new DataSet();
                    da = new SqlDataAdapter(sql, conn);
                    ds = new DataSet();
                    da.Fill(ds);
                    dataGridView1.DataSource = ds.Tables[0];
                }
        }

        private void rb_zx_CheckedChanged(object sender, EventArgs e)
        {
            string sql = "select WarnNo, WarNature,WarType,WarBegindate,WarContent,WarDosage,WarWay,WarTime,WarFinishdate,WarExecutedate from Warn where Warn.HosNo='" + this.tb_ZYNo.Text + "'and WarnCondition='执行'";
            if (rb_zx.Checked == true)


                using (SqlConnection conn = new SqlConnection(DBHelper.connString))
                {
                    SqlDataAdapter da = new SqlDataAdapter();
                    DataSet ds = new DataSet();
                    da = new SqlDataAdapter(sql, conn);
                    ds = new DataSet();
                    da.Fill(ds);
                    dataGridView1.DataSource = ds.Tables[0];
                }
        }

        private void rb_yt_CheckedChanged(object sender, EventArgs e)
        {
            string sql = "select WarnNo, WarNature,WarType,WarBegindate,WarContent,WarDosage,WarWay,WarTime,WarFinishdate,WarExecutedate from Warn where Warn.HosNo='" + this.tb_ZYNo.Text + "'and WarnCondition='已停'";
            if (rb_yt.Checked == true)


                using (SqlConnection conn = new SqlConnection(DBHelper.connString))
                {
                    SqlDataAdapter da = new SqlDataAdapter();
                    DataSet ds = new DataSet();
                    da = new SqlDataAdapter(sql, conn);
                    ds = new DataSet();
                    da.Fill(ds);
                    dataGridView1.DataSource = ds.Tables[0];
                }
        }

        private void button_sc_Click(object sender, EventArgs e)
        {
            string zno = this.tb_ZYNo.Text.Trim();
            string a = Convert.ToString(this.dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value).Trim();
            string b = Convert.ToString(this.dataGridView1[4, dataGridView1.CurrentCell.RowIndex].Value).Trim();
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                
             
                if (MessageBox.Show("确定要删除吗？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes)
                    return;

                string str = string.Format("delete from Warn  where Warn.HosNo='" + zno + "'and WarnNo='" + a + "'and WarContent='" + b + "'");
              
                SqlCommand com = new SqlCommand(str, conn);
      
                conn.Open();
                int m = Convert.ToInt32(com.ExecuteNonQuery());
          
                if ( m == 1 )
                {
                    MessageBox.Show("删除成功！");
                }
                else
                {
                    MessageBox.Show("删除失败！");
                }
                DataGridViewSelectedRowCollection mRowlnfo = this.dataGridView1.SelectedRows;
                foreach (DataGridViewRow dataGridViewRow in mRowlnfo)
                {
                    this.dataGridView1.Rows.RemoveAt(dataGridViewRow.Index);
                    dataGridViewRow.Selected = false;
                }
                this.dataGridView1.Refresh();
            }
        }

        private void button_tz_Click(object sender, EventArgs e)
        {

            string zno = this.tb_ZYNo.Text.Trim();
            using (SqlConnection conn = new SqlConnection(DBHelper.connString))
            {
                string a = Convert.ToString(this.dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value).Trim();
                string b = Convert.ToString(this.dataGridView1[4, dataGridView1.CurrentCell.RowIndex].Value).Trim();

                if (MessageBox.Show("确定要停止吗？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes)
                    return;

                string str = string.Format("update  Warn set WarnCondition='已停' where Warn.HosNo='" + zno + "'and WarnNo='" + a + "'and WarContent='" + b + "'");

                SqlCommand com = new SqlCommand(str, conn);

                conn.Open();
                int m = Convert.ToInt32(com.ExecuteNonQuery());

                if (m == 1)
                {
                    MessageBox.Show("医嘱停止成功！");
                }
                else
                {
                    MessageBox.Show("医嘱停止失败！");
                }
         
            }
           
        }

    
    }
}